package com.ayoka.ui.course.course_package;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.ayoka.Constant;
import com.ayoka.R;
import com.ayoka.adapter.CoursePackageCourseContentAdapter;
import com.ayoka.adapter.CoursePackageCourseStatusAdapter;
import com.ayoka.base.BaseRecyclerViewActivity;
import com.ayoka.bean.CourseBean;
import com.ayoka.bean.CoursePackageBean;
import com.ayoka.bean.ResponseCoursePackageDetailBean;
import com.ayoka.bean.UserInformation;
import com.ayoka.databinding.ActivityCoursePackageDetailBinding;
import com.ayoka.menu.CourseEstimateActionProvider;
import com.ayoka.mvp.CoursePackageMVP.CoursePackagePresenter;
import com.ayoka.mvp.CoursePackageMVP.ICoursePackageView;
import com.ayoka.ui.course.course_estimate.CourseEstimateDescriptionActivity;
import com.ayoka.ui.course.detail.CourseDetailActivity;
import com.ayoka.ui.customer_service.CustomerServiceRecyclerViewActivity;
import com.ayoka.ui.member.OpenMemberActivity;
import com.ayoka.ui.training.TrainingGroupActivity;
import com.ayoka.utils.CourseEvent;
import com.ayoka.utils.MemberEvent;
import com.jcp.baselibrary.brvah.BaseQuickAdapter;
import com.jcp.baselibrary.brvah.BaseViewHolder;
import com.jcp.baselibrary.model.ToolBarOptions;

import java.util.List;
import java.util.Observable;


/**
 * @author jcp
 * @ClassName: TopicListActivity
 * @Description:
 * @date 2018-12-28 下午4:21:18
 */
public class CoursePackageDetailActivity extends BaseRecyclerViewActivity<CourseBean, ActivityCoursePackageDetailBinding, CoursePackagePresenter> implements ICoursePackageView {

    public static void start(Activity activity, String packageName, long coursePackageId) {
        Intent intent = new Intent(activity, CoursePackageDetailActivity.class);
        intent.putExtra("PackageName", packageName);
        intent.putExtra("CoursePackageId", coursePackageId);
        activity.startActivity(intent);
    }

    private String packageName;
    private long coursePackageId;
    private CoursePackageBean coursePackageBean;

//    private HeaderCoursePackageDetailBinding headerBinding;

    @Override
    protected void configIntentValue() {
        Intent intent = getIntent();
        if (intent != null) {
            packageName = intent.getStringExtra("PackageName");
            coursePackageId = intent.getLongExtra("CoursePackageId", 0);
        }
    }

    @Override
    public int getContentLayout() {
        return R.layout.activity_course_package_detail;
    }

    @Override
    protected void initViews() {
        super.initViews();
        // 课程头部文字部分
//        headerBinding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.header_course_package_detail, null, false);
//        adapter.addHeaderView(headerBinding.getRoot());
//        adapter.setHeaderAndEmpty(true);

        mDataBinding.btnCustomerServiceConsultation.setOnClickListener(this);
        mDataBinding.btnOpenMember.setOnClickListener(this);
    }

    @Override
    protected SwipeRefreshLayout getRefreshLayout() {
        return mDataBinding.swipeRefresh;
    }

    @Override
    protected RecyclerView getRecyclerView() {
        return mDataBinding.recyclerViewContent;
    }

    @Override
    protected BaseQuickAdapter<CourseBean, BaseViewHolder> getAdapter() {
        CoursePackageCourseContentAdapter adapter = new CoursePackageCourseContentAdapter();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CourseBean courseBean = (CourseBean) adapter.getItem(position);
                if (courseBean.IsAvailabe == 0) {
                    showToast(getString(R.string.please_open_member));
                } else {
                    if (courseBean.CourseCompletedStatus != -1) {
                        CourseDetailActivity.start(CoursePackageDetailActivity.this, courseBean.ID);
                    } else {
                        showToast(getString(R.string.not_yet_start_course));
                    }
                }
            }
        });
        return adapter;
    }

    @Override
    protected boolean getLoadEnable() {
        return false;
    }

    @Override
    protected RecyclerView.ItemDecoration getRecycleDivider() {
        return null;
    }

    @Override
    protected void getData(boolean isShowWaitDialog) {
        mPresenter.getCoursePackageDetailData(coursePackageId);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        ToolBarOptions options = new ToolBarOptions(packageName);
        setCustomToolBar(mDataBinding.customToolbar.toolbar, options);

        CourseEvent.getInstance().addObserver(this);
        if (UserInformation.getInstance().user_info.IsVip == 0) {
            MemberEvent.getInstance().addObserver(this);
        }
        getData(true);
    }

    @Override
    protected CoursePackagePresenter getPresenter() {
        return new CoursePackagePresenter();
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        if (view == mDataBinding.btnCustomerServiceConsultation) {
            CustomerServiceRecyclerViewActivity.start(this);
        }
        if (view == mDataBinding.btnOpenMember) {
            OpenMemberActivity.start(this, Constant.OpenMember.OPEN_MEMBER);
        }
    }

    @Override
    public void configCoursePackageDetailData(ResponseCoursePackageDetailBean responseCoursePackageDetailBean) {
        if (responseCoursePackageDetailBean == null || responseCoursePackageDetailBean.course_package == null)
            return;
        this.coursePackageBean = responseCoursePackageDetailBean.course_package;
//        headerBinding.tvCoursePackageDescription.setText(coursePackageBean.Descp);
        setLoadMoreLayout(coursePackageBean.Courses);

        configCoursePackageStatus(coursePackageBean.Courses);

        if (responseCoursePackageDetailBean.coupon != null) {
            mPresenter.showUseCouponDialog(getSupportFragmentManager(), responseCoursePackageDetailBean.coupon.ID, responseCoursePackageDetailBean.coupon.Title);
        }

        if (responseCoursePackageDetailBean.course_package.IsAvailabe == 0) {
            mDataBinding.layoutBottomContainer.setVisibility(View.VISIBLE);
        } else {
            mDataBinding.layoutBottomContainer.setVisibility(View.GONE);
        }
    }

    private void configCoursePackageStatus(List<CourseBean> courses) {
        mDataBinding.recyclerViewTop.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        CoursePackageCourseStatusAdapter statusAdapter = new CoursePackageCourseStatusAdapter(courses);
        statusAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                CourseBean courseBean = (CourseBean) adapter.getItem(position);
                if (view.getId() == R.id.tv_course_status) {
                    if (courseBean.IsAvailabe == 0) {
                        showToast(getString(R.string.please_open_member));
                    } else {
                        if (courseBean.CourseCompletedStatus == -1) {
                            showToast(getString(R.string.not_yet_start_course));
                        } else {
                            CourseDetailActivity.start(CoursePackageDetailActivity.this, courseBean.ID);
                        }
                    }
                } else if (view.getId() == R.id.tv_training_status) {
                    if (courseBean.IsAvailabe == 0) {
                        showToast(getString(R.string.please_open_member));
                    } else {
                        if (courseBean.CourseCompletedStatus == -1) {
                            showToast(getString(R.string.not_yet_start_course));
                        } else if (courseBean.CourseCompletedStatus == 0) {
                            showToast(getString(R.string.not_yet_finish_course));
                        } else {
                            TrainingGroupActivity.start(CoursePackageDetailActivity.this, courseBean.ID, 0);
                        }
                    }
                }
            }
        });
        mDataBinding.recyclerViewTop.setAdapter(statusAdapter);
    }

    @Override
    public void onUseCouponCallback(boolean isSuccess) {
        if (isSuccess && mPresenter != null) {
            onRefresh();
        }
    }

    @Override
    public void update(Observable observable, Object data) {
        super.update(observable, data);
        if (observable instanceof CourseEvent) {
            if (mPresenter != null) {
                onRefresh();
            }
        }
        if (observable instanceof MemberEvent) {
            if (UserInformation.getInstance().user_info.IsVip == 0) {
                mDataBinding.layoutBottomContainer.setVisibility(View.VISIBLE);
            } else if (UserInformation.getInstance().user_info.IsVip == 1) {
                mDataBinding.layoutBottomContainer.setVisibility(View.GONE);
            }
            if (mPresenter != null) {
                onRefresh();
            }
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.course_package_detail_menu, menu);
        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.findItem(R.id.action_course_estimate);
        CourseEstimateActionProvider mActionProvider = (CourseEstimateActionProvider) MenuItemCompat.getActionProvider(menuItem);
        mActionProvider.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (view.getId() == R.id.tv_estimate){
                    CourseEstimateDescriptionActivity.start(CoursePackageDetailActivity.this, coursePackageId);
                }
            }
        });// 设置点击监听。
        return true;
    }

    @Override
    protected void onDestroy() {
        CourseEvent.getInstance().deleteObserver(this);
        MemberEvent.getInstance().deleteObserver(this);
        super.onDestroy();
    }
}
